﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.Threading
{
    /// <summary>
    /// Specifies a method pointer which is used in conjunction with the MessageQueueCall object.
    /// </summary>
    /// <typeparam name="TData">Type of call the argument which has been passed to the queue.</typeparam>
    /// <param name="argument">Call argument which has been passed to the queue.</param>
    public delegate void MessageQueueCallHandler<TData>(TData argument);

    /// <summary>
    /// Executes a message queue entry and consumes the given data object.
    /// </summary>
    /// <typeparam name="TData">Call arguments which are passed to the queue.</typeparam>
    public class MessageQueueCall<TData> : ICall<TData>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly MessageQueueCallHandler<TData> _handler = null;

        #endregion
   
        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new MessageQueueCall instance.
        /// </summary>
        /// <param name="handler">Method which should be executed.</param>
        public MessageQueueCall(MessageQueueCallHandler<TData> handler)
        {
            PreCondition.AssertNotNull(handler, "handler");

            _handler = handler;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Executes the queued item.
        /// </summary>
        /// <param name="data">Contains the data which is required to execute a worker item.</param>
        public void Execute(TData data)
        {
            using (Logger<MessageQueueCall<TData>>.StartNdc(System.Threading.Thread.CurrentThread.Name))
            {
                if (Logger<MessageQueueCall<TData>>.IsDebugEnabled)
                    Logger<MessageQueueCall<TData>>.DebugFormat(Resources.LogExecuteMessageQueueCall);

                try
                {
                    _handler(data);
                }
                finally
                {
                    if (Logger<MessageQueueCall<TData>>.IsDebugEnabled)
                        Logger<MessageQueueCall<TData>>.DebugFormat(Resources.LogExecutedMessageQueueCall);
                }
            }
        }

        #endregion
    }
}
